def cached_kernel(kernel, maxSize=10000000):
    cache = {}
    def kernel2(x, y, data, k=0, decay=0):
        if cache.has_key((x,y)):
            return cache[(x,y)]
        if cache.has_key((y,x)):
            return cache[(y,x)]
        #print "Run kernel"
        value = kernel(x, y, data, k, decay)
        if len(cache) == maxSize:
            cache.popitem()
        cache[(x,y)] = value
        return value
    return kernel2